from datetime import datetime, timedelta
from procdb import ProcessObject
from procutil import Task


"""
TODO: DAVID Add new classes in separate 
class DownloadCategories:
	def __init__(self,Fn):
		self.fn	= Fn
		
	def __call__(self,args):
		TODO: DAVID Handle the call.
"""

class ProcessManager:
	def __init__(self):
		self.oid		= None
		self.methods	= dict()
		self.maxTasks	= 10
		return

		
	def Run(self):
		count = 0
		tasks = Task().GetAllTasks()
		for tid in tasks:
			task	= Task()
			task.Load( tid )
			if self._RunTask(task)== True:
				count	= count+1
				
			if count == self.maxTasks:
				return

	def _RunTask(self,task):
		try:
			if task.status != 0:
				return False
				
			if self.methods.has_key(task.name) == False:
				task.status=-2
				task.Update()
				return	False
			
			Fn		= self.methods[task.name]
			
			if len(task.arg)>0:
				Fn( pickle.loads(task.arg) )
			else:
				Fn()
			
			return True
		except Exception, e:
			task.status=-1
			task.Update()
			print "Print"
		
	def Load(self):
		# TODO: Load all methods supported
		self.methods["name0"]=""
		self.methods["name3"]=""
		
		# Add a separate class to handle this request
		#self.methods["Directory.DownloadCategories"]=""
		#self.methods["Directory.DownloadCompanies"]=""
		#self.methods["Directory.DownloadCompanyProfile"]=""
		#self.methods["Directory.DownloadCompanyProfile"]=""
		#self.methods["Website.GetSiteInfo"]=""
		return
		
